/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ClassFrame;

import GamePlay.CanvasGame;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.game.Sprite;

/**
 *
 * @author QuyetNM1
 */
public class Image {

    public static final int LEFT = 0;
    public static final int CENTER = 1;
    public static int[] num = new int[20];

    public Image() {
    }

    public void drawImage(Graphics g, CanvasGame canvas, int Image, int x, int y) {
        g.drawImage(canvas.getResource().getIMG(Image), x, y, Graphics.TOP | Graphics.LEFT);
    }

    public void drawImage(Graphics g, CanvasGame canvas, int Image, int x, int y, int anchor) {
        g.drawImage(canvas.getResource().getIMG(Image), x, y, anchor);
    }

    public void drawImagePart(Graphics g, CanvasGame canvas, int Image, int x, int y, int x_dest, int y_dest, int w, int h) {
//        int wImg = canvas.getResource().getIMG(Image).getWidth();
//        int hImg = canvas.getResource().getIMG(Image).getHeight();
//        if(x_dest > wImg || y_dest > hImg){
//            System.out.println("Out of size of Image");
//            return;
//        }
//        if(x_dest + w > wImg){
//            w = wImg - x_dest;
//        }
//        if(y_dest + h > hImg){
//            h = hImg - y_dest;
//        }
        g.drawRegion(canvas.getResource().getIMG(Image), x_dest, y_dest, w, h, Sprite.TRANS_NONE, x, y, Graphics.TOP | Graphics.LEFT);
    }

    public void drawImagePart(Graphics g, CanvasGame canvas, int Image, int x, int y, int x_dest, int y_dest, int w, int h, int anchor) {
        int wImg = canvas.getResource().getIMG(Image).getWidth();
        int hImg = canvas.getResource().getIMG(Image).getHeight();
        if (x_dest > wImg || y_dest > hImg) {
            System.out.println("Out of size of Image");
            return;
        }
        if (x_dest + w > wImg) {
            w = wImg - x_dest;
        }
        if (y_dest + h > hImg) {
            h = hImg - y_dest;
        }
        g.drawRegion(canvas.getResource().getIMG(Image), x_dest, y_dest, w, h, Sprite.TRANS_NONE, x, y, anchor);
    }

    public void drawImagePosition(Graphics g, CanvasGame canvas, int Image, int x, int y, int p_w, int p_h) {
        g.drawImage(canvas.getResource().getIMG(Image), x, y, p_w | p_h);
    }

    public void drawNumber(Graphics g, CanvasGame canvas, int Image, int number, int x, int y, int anchor) {
        if(number < 0) return;
        int i = 0;
        int j = 0;
        int w = canvas.getResource().getIMG(Image).getWidth() / 10;
        int h = canvas.getResource().getIMG(Image).getHeight();
        int n = number;

        if (n == 0) {
            i = 1;
            num[0] = 0;
        } else {
            while (n != 0) {
                if (i == 19) {
                    break;
                }
                num[i++] = n % 10;
                n = n / 10;
            }
        }
        for (j = i - 1; j > -1; --j) {
            g.drawRegion(canvas.getResource().getIMG(Image), w * num[j], 0, w, h, Sprite.TRANS_NONE, x + w * (i - j - 1) - anchor * i * w / 2, y, Graphics.TOP | Graphics.LEFT);
        }
    }

    public javax.microedition.lcdui.Image resizeImg(javax.microedition.lcdui.Image input, int x2, int y2) {
        int x = input.getWidth();
        int y = input.getHeight();
        int ini[] = new int[input.getWidth() * input.getHeight()];
        //Get the RGB array of image into "rgb" 

        int i = 0, t2;
        int dy, dx;
        input.getRGB(ini, 0, input.getWidth(), 0, 0, input.getWidth(), input.getHeight());
        int out[] = new int[x2 * y2];
        //t1=y/y2; 
        t2 = x / x2;
        for (int yy = 0; yy < y2; yy++) {
            dy = yy * y / y2;

            for (int xx = 0; xx < x2; xx++) {
                dx = xx * t2;

                out[i++] = ini[(x * dy) + dx]; //(x2*yy)+xx 
            }
        }
        javax.microedition.lcdui.Image temp3 = javax.microedition.lcdui.Image.createRGBImage(out, x2, y2, true);
        return temp3;
    }
}
